package com.eagle.platform.api.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.eagle.common.entity.po.AdminResources;
import com.eagle.common.entity.po.AdminUser;
import com.eagle.common.entity.po.ParentAdminResources;
import com.eagle.common.entity.po.query.AdminResourcesQuery;
import com.eagle.common.entity.po.query.AdminUserQuery;
import com.eagle.common.entity.vo.PaginationResult;
import com.eagle.common.exception.BusinessException;
import com.eagle.common.service.AdminResourcesService;
import com.eagle.common.util.JacksonUtils;
import com.eagle.platform.api.constant.APIResultCodeEnum;



/**
 * 
 * 访问资源表 控制层
 * 
 */
@Controller
@RequestMapping("adminResources")
public class AdminResourcesController extends BaseController{

	@Resource
	private AdminResourcesService adminResourcesService;

	/**
	 * 查询所有资源或者单个资源信息
	 * param id (资源主键)
	 */
	@RequestMapping(value = "list", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String list(@RequestBody(required = false) String id) {
		List<ParentAdminResources> parentAdminResources=null;
		AdminResourcesQuery query = null;
		if(!StringUtils.isAllBlank(id)) {
			query=JacksonUtils.fromJson(id,AdminResourcesQuery.class);
		}
		parentAdminResources=adminResourcesService.selectParent(query);
		if (query == null){
            query = new AdminResourcesQuery();
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, parentAdminResources));
	}
	
	/**
	 * 添加资源
	 * param adminResources (资源表)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "insert", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String insert(@RequestBody(required = false) AdminResources adminResources) throws BusinessException {
		Integer query = null;
		if(adminResources!=null) {
			query=adminResourcesService.add(adminResources);
		}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	/**
	 * 修改资源信息
	 * param adminResources (资源表)
	 * param id (资源表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "updateById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String updateById(@RequestBody(required = false)AdminResources adminResources) throws BusinessException {
		Integer query = null;
		Integer id=adminResources.getId();
		if(adminResources!=null && id!=null) {
			query=adminResourcesService.updateById(adminResources, id);
		}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	/**
	 * 删除资源信息
	 * param id (资源表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "deleteById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String deleteById(@RequestBody(required = false)String id) throws BusinessException {
		Integer value = null;
		AdminResourcesQuery query = null;
		if(id!=null) {
			query=JacksonUtils.fromJson(id,AdminResourcesQuery.class);
			value=adminResourcesService.deleteById(query.getId());
		}
		if (value == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
}